package libraries;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class readData {
	public static int xlRows, xlCols; 
	public static InputStream input = null;
	public static XSSFWorkbook workBook = null;
	public static XSSFSheet sheet = null;
	public static XSSFRow row = null;
	public static XSSFCell cell = null;
	public static String[][] xldata;
	public static String[][] xlerr =null;
	
	public readData(String fileName)throws IOException{
	input = new FileInputStream(fileName);
	workBook = new XSSFWorkbook(input);
	input.close();
			
}

public int getRowCount(String sheetName) throws IOException{
	sheet = workBook.getSheet(sheetName);
		return sheet.getPhysicalNumberOfRows();
	
}

public int getColCount(String sheetName) throws IOException{
	sheet = workBook.getSheet(sheetName);
	return sheet.getRow(1).getPhysicalNumberOfCells();
}

public String getStringCellValue(String sheetName, int ro, int cel) throws IOException{
	try {
	sheet = workBook.getSheet(sheetName);
	//return sheet.getRow(row).getCell(cell).getStringCellValue();
	
	return sheet.getRow(ro).getCell(cel).toString();
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		return "Fail " + e.getMessage();
	}
}

public static void wait_time(Long fWait) throws Exception{
	Thread.sleep(fWait);
}

public  String[][] xlRead(String sheetName) throws IOException{
	try {
		sheet = workBook.getSheet(sheetName);
		xlRows = sheet.getPhysicalNumberOfRows();
		xlCols = sheet.getRow(1).getPhysicalNumberOfCells();
		if(sheetName.equals("TestCases"))
			{xlCols = 4;}	
	xldata = new String[xlRows][xlCols];
      for (int i = 0; i < xlRows; i++) {
           // row = sheet.getRow(i);
            for (int j = 0; j < xlCols; j++) {
                //cell = row.getCell(j); // To read value from each col in each row
               String value = sheet.getRow(i).getCell(j).toString();
               xldata[i][j] = value;
               //System.out.println(xldata[i][j]);
              }
          }
      return xldata;
	    }
	catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println("Fail " + e.getMessage());
		return xlerr;
	}
}


  }